<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>更新</title>
    <div th:replace="header::common_resource"></div>
</head>
<body>

    <form id="formId" class="layui-form layui-form-pane" action="">
        <input type="hidden" name="id" th:value="${product.id}"/>
        <div class="layui-form-item">
            <label class="layui-form-label">商品名字</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" th:value="${product.name}" th:placeholder="${product.name}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品子标题</label>
            <div class="layui-input-block">
                <input type="text" name="subtitle" autocomplete="off" th:value="${product.subtitle}" th:placeholder="${product.subtitle}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品分类</label>
            <div class="layui-input-inline">
                <select id="topCategory"  lay-filter="topFilter">
                    <option th:value="${topCategory.id}" th:text="'[当前]'+${topCategory.name}"></option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select id="secCategory" name="categoryId" lay-filter="secFilter">
                    <option th:value="${curCategory.id}" th:text="'[当前]'+${curCategory.name}"></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属专区id</label>
            <div class="layui-input-inline">
                <select id="zon_id" name="zone"  >
                    <option th:value="${product.zone}" th:text="'[原]'+${product.zone}"></option>
                    <option value="0">0(无)</option>
                    <option value="1">1(热卖专区)</option>
                    <option value="2">2(满减专区)</option>
                    <option value="3">3(折扣专区)</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品价格</label>
            <div class="layui-input-block">
                <input type="text" name="price" autocomplete="off" th:value="${product.price}" th:placeholder="${product.price}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品库存</label>
            <div class="layui-input-block">
                <input type="text" name="stock" autocomplete="off" th:value="${product.stock}" th:placeholder="${product.stock}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" th:title="在售" th:checked="${product.status==1}"/>
                <input type="radio" name="status" value="0" th:title="下架" th:checked="${product.status==0}"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品图片</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <div class="layui-upload-list">
                        <img width="150px" height="150px" class="layui-upload-img" id="demo1" th:src="${product.mainImage}">
                        <input type="hidden" name="mainImage" id="imageId" th:value="${product.mainImage}" >
                        <p id="demoText"></p>
                    </div>
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品详情</label>
            <div class="layui-input-block">
                <textarea id="content"  name="detail" th:utext="${product.detail}"  placeholder="请输入内容" class="layui-textarea" style="height: 400px"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" onclick="submitForm()" class="layui-btn" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script th:src="@{/static/kindeditor/kindeditor.js}" charset="utf-8"></script>
    <script th:inline="javascript">
        KindEditor.ready(function() {
            var kindEditorParams = {
                afterBlur: function() {
                    this.sync();
                }
            };
            KindEditor.create('#content', kindEditorParams);
        });
        layui.use(['form'], function () {
            var form = layui.form;
            $.post(
                [[${#request.getContextPath()}]]+'/category/selectAllTop',
                function (result) {
                    // console.log(result);
                    $(result).each(function () {
                        $('#topCategory').append('<option value="'+this.id+'">'+this.name+'</option>');
                        layui.form.render('select');
                    });
                },
                'json'
            );

            form.on('select(topFilter)', function(data){
                var topId=data.value;
                $('#secCategory').empty();
                $('#secCategory').append('<option value="">请选择主要分类</option>');
                if(topId==null||topId==''){
                    layui.form.render('select');
                    return;
                }
                //console.log(buildingId);
                $.post(
                    [[${#request.getContextPath()}]]+'/category/selectAllSec',
                    {'topId': topId},
                    function (jsonObj) {
                        // console.log(jsonObj)
                        $(jsonObj).each(function () {
                            $('#secCategory').append('<option value="'+this.id+'">'+this.name+'</option>');
                            layui.form.render('select');
                        });
                    },
                    'json'
                );
            });

        });
        layui.use(['upload'], function() {
            var upload = layui.upload;

            //常规使用 - 普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                ,url: [[${#request.getContextPath()}]]+'/upload/uploadImage'
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                ,done: function(result){
                    if (result.code == 0) {
                        $('#imageId').val(result.data);
                        $('#demoText').html(''); //置空上传失败的状态
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });

        });


        function submitForm() {
            $.post(
                [[${#request.getContextPath()}]]+'/product/update',
                $('#formId').serialize(),
                function(result) {
                    console.log(result);
                    if (result.code == 0) {
                        mylayer.okMsg(result.msg)
                        setInterval(function() {
                            //关闭弹出框
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            //刷新父页面
                            window.parent.location.reload();
                        }, 2000)
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                },
                'json'
            );
        }
    </script>
</body>
</html>